package com.ricky.handler.authorise;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ricky.config.Lan;
import com.ricky.core.net.abs.Request;
import com.ricky.core.net.msg.Context;
import com.ricky.core.net.utils.Attributes;
import com.ricky.db.model.HeroData;
import com.ricky.game.GameWorld;
import com.ricky.game.Hero;
import com.ricky.game.MsgHelper;
import com.ricky.handler.IHandler;

/**
 * 创建英雄
 * 
 * @author rik
 * 
 */
public class CreateHero implements IHandler {

	protected static Logger log = Logger.getLogger(CreateHero.class);

	@SuppressWarnings({ "rawtypes" })
	@Override
	public void proccess(Request request,  Map<String,Object> context) {

		if (request.getAttribute(Attributes.USER_Name) == null) // 非法登陆
		{
			MsgHelper.sendTips(request, Lan.getValue("login_break-in"));
		} else {
			String userName = request.getAttribute(Attributes.USER_Name)
					.toString();
			Map hero = (Map) context;
			String unitID = (String) hero.get("unitID");
			String heroName = (String) hero.get("heroName");

			HeroData hd = new HeroData();

			Hero h = new Hero();
			h.setName(heroName);
			h.setSceneID(1);
			h.setUserName(userName);
			h.setUnit(unitID);

			HeroData.copyTo(h, hd);

			request.setAttribute(Attributes.HERO, h);
			GameWorld.getInstance().joinWorld(h, request);
			
			
			Map msg=new HashMap();
			msg.put("cmd", "l.setHero");
			msg.put("hero", h);
			MsgHelper.sendMsg(h.getName(), msg);
			
			request.isValidate(true);
		}

	}

}
